import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useAppStore = defineStore('app', () => {
  // 状态
  const collapsed = ref(false) // 侧边栏是否收起
  const loading = ref(false) // 全局加载状态
  const theme = ref('dark') // 主题模式
  const breadcrumb = ref([]) // 面包屑导航

  // 系统信息
  const systemInfo = ref({
    version: '1.0.0',
    databaseVersion: 'MySQL 8.0.35',
    serverTime: new Date().toLocaleString('zh-CN'),
    uptime: '30天 12小时 45分钟',
    memoryUsage: '2.4GB / 8GB (30%)',
    diskUsage: '45GB / 100GB (45%)',
    onlineUsers: 168,
    totalSouls: 1234567,
    todayJudgments: 89,
    pendingCases: 156
  })

  // 切换侧边栏收起状态
  const toggleCollapsed = () => {
    collapsed.value = !collapsed.value
    localStorage.setItem('sidebarCollapsed', collapsed.value.toString())
  }

  // 设置加载状态
  const setLoading = (status) => {
    loading.value = status
  }

  // 切换主题
  const toggleTheme = () => {
    theme.value = theme.value === 'dark' ? 'light' : 'dark'
    localStorage.setItem('theme', theme.value)
  }

  // 设置面包屑
  const setBreadcrumb = (items) => {
    breadcrumb.value = items
  }

  // 更新系统信息
  const updateSystemInfo = (info) => {
    systemInfo.value = { ...systemInfo.value, ...info }
  }

  // 初始化应用状态
  const initAppState = () => {
    // 恢复侧边栏状态
    const savedCollapsed = localStorage.getItem('sidebarCollapsed')
    if (savedCollapsed !== null) {
      collapsed.value = savedCollapsed === 'true'
    }

    // 恢复主题设置
    const savedTheme = localStorage.getItem('theme')
    if (savedTheme) {
      theme.value = savedTheme
    }
  }

  return {
    // 状态
    collapsed,
    loading,
    theme,
    breadcrumb,
    systemInfo,

    // 方法
    toggleCollapsed,
    setLoading,
    toggleTheme,
    setBreadcrumb,
    updateSystemInfo,
    initAppState
  }
})
